package com.yonyougov.devops.cruxcisupport.userrole;

import com.yonyougov.devops.cruxcisupport.userrole.dto.controller.UserRoleAddBatchRequestDto;
import com.yonyougov.devops.cruxcisupport.userrole.dto.controller.UserRoleByRoleParamPageLikeRequestDto;
import com.yonyougov.devops.cruxcisupport.userrole.dto.controller.UserRoleByUserParamPageLikeRequestDto;
import com.yonyougov.devops.cruxcisupport.userrole.dto.controller.UserRoleDeleteBatchRequestDto;
import com.yonyougov.devops.cruxcisupport.userrole.dto.service.UserPageByRoleRaramLikeDto;
import com.yonyougov.devops.cruxcisupport.userrole.dto.service.UserRoleAddBatchDto;
import com.yonyougov.devops.cruxcisupport.userrole.dto.service.UserRoleByUserParamPageLikeDto;
import com.yonyougov.devops.cruxcisupport.userrole.vo.UserRoleByRoleParamPageLikeVo;
import com.yonyougov.devops.cruxcisupport.userrole.vo.UserRoleByUserParamPageLikeVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(value = "用户角色模块", tags = "用户角色模块")
@RestController
@RequestMapping("/api/v2/userRole")
public class UserRoleController extends CrudController<UserRoleEntity, BigInteger> {

    private final UserRoleService userRoleService;

    public UserRoleController(UserRoleService userRoleService) {
        this.userRoleService = userRoleService;
    }

    @Override
    public BaseService<UserRoleEntity, BigInteger> getService() {
        return userRoleService;
    }

    //<!--用户自定义代码开始-->

    @ApiOperation(value = "前端:用户批量关联角色调用", notes = "用户批量关联角色调用")
    @PostMapping(value = "/ur/add/batch")
    public BaseResponseDto addBatchUserRole(@RequestBody @Valid UserRoleAddBatchRequestDto request) {
        UserRoleAddBatchDto userRoleAddBatchDto = UserRoleAddBatchDto.builder().build();
        BeanUtils.copyProperties(request, userRoleAddBatchDto);

        userRoleService.addMulUserRole(userRoleAddBatchDto);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端:批量删除用户角色关联", notes = "批量删除用户角色关联")
    @PostMapping(value = "/ur/delete/batch/ids")
    @SneakyThrows
    public BaseResponseDto DeleteBatchUserRoleByIds(@RequestBody @Valid UserRoleDeleteBatchRequestDto request) {

        userRoleService.deleteBatchUserRoleByIds(request.getIds());

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端:用户关联角色的列表展示", notes = "可选择根据用户id和角色名称分页模糊查询用户角色关联")
    @PostMapping(value = "/ur/page/like")
    public BaseResponseDto findByPageLike(@RequestBody @Valid UserRoleByUserParamPageLikeRequestDto request) {
        UserRoleByUserParamPageLikeDto userRoleByUserParamPageLikeDto = new UserRoleByUserParamPageLikeDto();
        BeanUtils.copyProperties(request, userRoleByUserParamPageLikeDto);

        Pagination<UserRoleByUserParamPageLikeVo> pagination = userRoleService.findByUserParamPageLike(userRoleByUserParamPageLikeDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", pagination.getTotalCount());
        result.put("data", pagination.getDatas());
        return result;
    }

    @ApiOperation(value = "前端:角色关联用户的列表展示", notes = "可选择根据角色id和用户名称分页模糊查询用户角色关联")
    @PostMapping(value = "/ur/user/page")
    public BaseResponseDto findUserPageLikeByRoleId(@RequestBody @Valid UserRoleByRoleParamPageLikeRequestDto request) {
        UserPageByRoleRaramLikeDto userPageByRoleRaramLikeDto = new UserPageByRoleRaramLikeDto();
        BeanUtils.copyProperties(request, userPageByRoleRaramLikeDto);

        Pagination<UserRoleByRoleParamPageLikeVo> pagination = userRoleService.findByRoleParamPageLike(userPageByRoleRaramLikeDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", pagination.getTotalCount());
        result.put("data", pagination.getDatas());
        return result;
    }
    //<!--用户自定义代码结束-->
}
